﻿using System;
using System.Collections.Generic;
using System.Text;
using emp.IDAL;
using emp.Model;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace emp.SqlServerDAL
{
    public class NurturingGifts : INurturingGifts
    {

        #region INurturingGifts 成员

        public IList  getNurturingGifts(string[] parm)
        {
            IList dataList = new ArrayList();

            string sql = " select ING_UID, ING_IND_URN, ING_TYPE,ING_DATE, ING_STATUTS, ING_GIFT_DATE,IND_NAME, IND_CELLPHONE_1, IND_EMAIL_1, IND_PROVINCE,IND_CITY, IND_CITY_OTHER, IND_DISTRICT_1, IND_ADDRESS_1,IND_POSTCODE_1 from V_IND_NURTURING_GIFTS where 1=1 ";

            if (!String.IsNullOrEmpty(parm[0]))
            {
                sql += " and IND_NAME like ('%" + parm[0] + "%')";
            }
            if (!String.IsNullOrEmpty(parm[1]))
            {
                sql += " and IND_PROVINCE like ('%" + parm[1] + "%')";
            }
            if (!String.IsNullOrEmpty(parm[2]))
            {
                sql += " and IND_CITY like ('%" + parm[2] + "%')";
            }
            if (!String.IsNullOrEmpty(parm[3]))
            {
                sql += " and IND_CELLPHONE_1 like ('%" + parm[3] + "%')";
            }
            if (!String.IsNullOrEmpty(parm[4]))
            {
                sql += " and ING_DATE >='" + DateTime.Parse(parm[4]) + "'";
            }
            if (!String.IsNullOrEmpty(parm[5]))
            {
                sql += " and ING_DATE <'" + DateTime.Parse(parm[5]).AddDays(1) + "'";
            }


            using (SqlDataReader sdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, sql, null))
            {
                while (sdr.Read())
                {
                    
                    NurturingGiftsInfo nGifts = new NurturingGiftsInfo();
                    if (!sdr.IsDBNull(0)) nGifts.ING_UID = sdr.GetInt32(0);
                    if (!sdr.IsDBNull(1)) nGifts.ING_IND_URN = sdr.GetInt32(1);
                    if (!sdr.IsDBNull(2))
                    {
                        nGifts.ING_TYPE = "";

                        if (sdr.GetString(2).Equals("1"))
                        {
                            nGifts.ING_TYPE = "U盘";
                        }
                        else if (sdr.GetString(2).Equals("2"))
                        {
                            nGifts.ING_TYPE = "Audi杂志";
                        }

                    } 
                    if (!sdr.IsDBNull(3)) nGifts.ING_DATE = sdr.GetDateTime(3);
                    if (!sdr.IsDBNull(4)) nGifts.ING_STATUTS = sdr.GetString(4);
                    
                    if (!sdr.IsDBNull(5)) nGifts.ING_GIFT_DATE = sdr.GetDateTime(5);
                    if (!sdr.IsDBNull(6)) nGifts.IND_NAME = sdr.GetString(6);
                    if (!sdr.IsDBNull(7)) nGifts.IND_CELLPHONE_1 = sdr.GetString(7);
                    if (!sdr.IsDBNull(8)) nGifts.IND_EMAIL_1 = sdr.GetString(8);
                    if (!sdr.IsDBNull(9)) nGifts.IND_PROVINCE = sdr.GetString(9);
                    if (!sdr.IsDBNull(10)) nGifts.IND_CITY = sdr.GetString(10);
                    if (!sdr.IsDBNull(11)) nGifts.IND_CITY_OTHER = sdr.GetString(11);
                    if (!sdr.IsDBNull(12)) nGifts.IND_DISTRICT_1 = sdr.GetString(12);
                    if (!sdr.IsDBNull(13)) nGifts.IND_ADDRESS_1 = sdr.GetString(13);
                    if (!sdr.IsDBNull(14)) nGifts.IND_POSTCODE_1 = sdr.GetString(14);


                    dataList.Add(nGifts);
                }
            }

            return dataList;
        }

        #endregion

        public DataTable getGenNurturingLogList(int inluid)
        {
            string sqlstr = "select * from IND_NURTURING_BATCH where 1=1 ";
            if (inluid > 0)
            {
                sqlstr += " and INB_UID="+inluid; 
            }

            sqlstr += " order by INB_UID desc";

            DataSet data = new DataSet();
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                try
                {
                    sda.SelectCommand = new SqlCommand();
                    sda.SelectCommand.CommandText = sqlstr;
                    sda.SelectCommand.Connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
                    sda.Fill(data);
                }
                catch { }
            }
            return data.Tables[0];
        }

        public DataTable IsalreadyGened()
        {
            string sqlstr = "select 1 from IND_NURTURING_BATCH where convert(varchar(10),INB_GENERATE_DATE,121)=convert(varchar(10),getdate(),121) ";            

            DataSet data = new DataSet();
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                try
                {
                    sda.SelectCommand = new SqlCommand();
                    sda.SelectCommand.CommandText = sqlstr;
                    sda.SelectCommand.Connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
                    sda.Fill(data);
                }
                catch { }
            }
            return data.Tables[0]; 
        }
    }
}
